package com.example.weiyujie

import android.os.Bundle
import android.widget.LinearLayout
import android.widget.TextView
import androidx.activity.ComponentActivity
import android.graphics.Color

class HistoryActivity : ComponentActivity() {
    private lateinit var historyDisplay: LinearLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_history)

        historyDisplay = findViewById(R.id.historyDisplay)
        val history = readHistoryFromFile()
        displayHistory(history)
    }

    private fun readHistoryFromFile(): List<String> {
        return try {
            openFileInput("history.txt").bufferedReader().useLines { lines ->
                lines.toList()
            }
        } catch (e: Exception) {
            listOf("No history available.")
        }
    }

    private fun displayHistory(history: List<String>) {
        history.forEach { entry ->
            val historyTextView = TextView(this)
            historyTextView.text = entry
            historyTextView.textSize = 18f
            historyTextView.setTextColor(Color.RED)
            historyTextView.gravity = android.view.Gravity.END
            historyDisplay.addView(historyTextView)
        }
    }
}